//
//  CancellationByCodeVC.m
//  NtoccShipper
//
//  Created by mac on 2020/1/10.
//  Copyright © 2020 zhuchongjin. All rights reserved.
//

#import "CancellationByCodeVC.h"
#import "CancellationByPWVC.h"
  #import "LogInVC.h"
@interface CancellationByCodeVC (){
    int time;
}
//@property (copy, nonatomic) NSArray *arrTitles;
//@property (strong, nonatomic) NSArray *arrContent;
@property (strong, nonatomic) CJButton *btnSave;
@property (nonatomic,strong) CJButton *btnGetCode;
@property (nonatomic,strong) CJButton *btnAnother;
@end

@implementation CancellationByCodeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:CJLString(@"验证中心")];
    [self setBackLeftButton:@""];
    [self createUI];
    [self.view addSubview:self.btnSave];
}

- (void)createUI{
    
    NSArray *list = @[@"请填写手机号",@"请填写验证码",@""];
    for (int i = 0; i < list.count; i++) {
        
        UIView * liebiaoView = [[UIView alloc]initWithFrame:CGRectMake(0,kTopHeight + CJToViewMarginH+ i * 44, kScreenWidth, 44)];
        liebiaoView.backgroundColor = [UIColor cjWhiteColor];
        [self.view addSubview:liebiaoView];
        
        if (i <= 1) {
            CJTextField *field = CJTextField.tfInit
            .tfFrame(CGRectMake( CJToLeftMargin, 5, kScreenWidth - 2*CJToLeftMargin - 60, 35))
            .tfplaceholder(list[i])
//            .tfText(self.strPhoneNum)
            ;
            field.tag = 1000+ i;
            [liebiaoView addSubview:field];
            
            if (i == 0) {
                field.text = self.strPhoneNum;
                field.enabled = NO;
                self.btnGetCode = CJButton.btnInit
                .btnFrame(CGRectMake(field.right, LW(10), LW(55), liebiaoView.height - LW(20)))
                .btnBgColor([UIColor cjMainColor])
                .btnNormalTitleColor([UIColor cjWhiteColor])
                .btnTargetAction(self,@selector(btnGetAouthCode:))
                .btnCornerRadius(3)
                .btnText(@"验证码")
                ;
                [liebiaoView addSubview:self.btnGetCode];
            }
        }else{
            
            self.btnAnother = CJButton.btnInit
            .btnFrame(CGRectMake(kScreenWidth - 100 - CJToLeftMargin, LW(10), LW(100), liebiaoView.height - LW(20)))
//            .btnBgColor([UIColor cjMainColor])
            .btnNormalTitleColor([UIColor cjMainColor])
            .btnTargetAction(self,@selector(btnAnotherAction:))
            .btnCornerRadius(3)
            .btnText(@"切换验证方式")
            ;
            [liebiaoView addSubview:self.btnAnother];
        }
        
        UILabel * bg = [[UILabel alloc]initWithFrame:CGRectMake(CJToLeftMargin, liebiaoView.height -1, SCREEN_WIDTH, 1)];
        bg.backgroundColor = [UIColor cjBackgroundColor];
        [liebiaoView addSubview:bg];
    }
}

- (void)btnSaveAction{
//    UITextField *filed1 = [self.view viewWithTag:1000];
    UITextField *filed2 = [self.view viewWithTag:1001];
    
    if ([CJRegular getSMFAouth:filed2.text]) {
        
    }else{
        [CJAlert showStringDismissWithDelay:@"验证码格式不对"];
        return;
    }
    
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/party/confirmBye",CJBaseUrl);
    NSDictionary *dicPar = @{@"type":@"phone",
                             @"verifyCode":filed2.text,
    };
    
    @weakify(self);
    [[HDNetworking sharedHDNetworking] DELETE:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
        [CJControl clearAllUserDefaultsData];

//        DLog(@"++++++++%@",responseObject);
        @strongify(self);
        LogInVC *vc = [[LogInVC alloc]init];
        vc.isRegsiter = YES;
        [self pushVc:vc];
       
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
    }];
}

- (void)btnAnotherAction:(UIButton *)sender{
    CancellationByPWVC *vc = [CancellationByPWVC new];
    vc.strPhoneNum = self.strPhoneNum;
    [self pushVc:vc];
}
- (void)btnGetAouthCode:(UIButton *)sender{
    
    UITextField *filed1 = [self.view viewWithTag:1000];

    if (IsStrEmpty(filed1.text) || ![CJRegular getcheckPhone:filed1.text]) {
         [[[AFViewShaker alloc] initWithView:filed1] shakeWithDuration:[ShakerTime floatValue] completion:^{
             [CJAlert showStringDismissWithDelay:@"请填写正确的手机号"];

         }];
    }else{
    
        NSString *strURL = CJStringWithFormat(@"%@/ntocc-tps-api/sms/sendVerificationCode",CJBaseUrl);
        NSDictionary *dicPar = @{@"phone":CJStringWithFormat(@"%@",filed1.text),
                                 @"partyGroupCode":[CJControl getBranchCode],
                                };
        @weakify(self);
        [[HDNetworking sharedHDNetworking] POST:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
            
//            DLog(@"++++++++%@",responseObject);
            @strongify(self);
            sender.userInteractionEnabled = NO;
            sender.backgroundColor = [UIColor cjLineBackGroundColor];
            time = 60;
            [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerFireM:) userInfo:nil repeats:YES];
            
        } failure:^(NSError * _Nonnull error) {
            DLog(@"++++++++%@",error);
        }];
    }
}

- (void)timerFireM:(NSTimer *)theTimer{
    
    NSString * str =[NSString stringWithFormat:@"(%ds)",time];
    [self.btnGetCode setTitle:str forState:UIControlStateNormal];
    time--;
    if (time==0) {
        [theTimer invalidate];
        time=60;
        [self.btnGetCode setTitle:@"验证码" forState:UIControlStateNormal];
        self.btnGetCode.userInteractionEnabled = YES;
        self.btnGetCode.backgroundColor = [UIColor cjMainColor];
    }
}

#pragma mark ----------lazy
- (CJButton *)btnSave{
    if (!_btnSave) {
        _btnSave = CJButton.btnInit
        .btnFrame(CGRectMake(1*CJToLeftMargin, kTopHeight +  LW(170), SCREEN_WIDTH - 2*CJToLeftMargin, LW(40)))
        .btnFont([UIFont cjTitleFont17])
        .btnText(@"确认")
        .btnBgColor([UIColor cjMainColor])
        .btnCornerRadius(5)
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnTargetAction(self, @selector(btnSaveAction))
       
        ;
    }
    return _btnSave;
}

@end
